<?php
    /*
        数据库访问与操作都在此
        关乎数据安全，修改请慎重
    */
    //获取本比赛的记录
    function mGetMatchItemById($id){
        return mfa(mq("select * from data_match where id=".intval($id)));
    }
    //获取本比赛的相邻上一个比赛id
    function mGetMatchPreId($id){
        $row=mfa(mq("select * from data_match where id<".intval($id)." order by id desc limit 0,1"));
        return $row[id];
    }
    //获取本比赛的相邻下一个比赛id
    function mGetMatchNextId($id){
        $row=mfa(mq("select * from data_match where id>".intval($id)." order by id limit 0,1"));
        return $row[id];
    }
    //如果不存在我的微信用户记录则创建
    function mCreateMyGuestIfNotExist(){
        if(!mfa(mq("select * from data_guest where openid='$_SESSION[openid]'"))){
            mq("insert into data_guest(openid) values ('$_SESSION[openid]')");
        }
    }
    //返回我的微信用户记录
    function mGetMyGuestItem(){
        return mfa(mq("select * from data_guest where openid='$_SESSION[openid]'"));
    }
    //返回微信用户记录
    function mGetGuestItem($gid){
        return mfa(mq("select * from data_guest where gid=$gid"));
    }
    //如果不存在我的本次选手记录则创建
    function mCreateMyPlayerIfNotExist($mid){
        if(!mfa(mq("select * from data_player where `mid`='$mid' and openid='$_SESSION[openid]'"))){
            mq("insert into data_player(`mid`,openid,邀请人,积分) values ('$mid','$_SESSION[openid]','$_SESSION[frompid]','100')");
            //我参赛，我邀请人得 20 积分
            if($_SESSION[frompid]>0){ mAddPlayerPoint($_SESSION[frompid],20); }
        }
    }
    //返回我的本次比赛的记录
    function mGetMyPlayerItem($mid){
        return mfa(mq("select * from data_player where `mid`='$mid' and openid='$_SESSION[openid]'"));
    }
    //返回比赛选手的记录
    function mGetPlayerItem($pid){
        return mfa(mq("select * from data_player where id='$pid'"));
    }
    //根据gid和matchid返回比赛选手的记录
    function mGetPlayerItemByGidMid($gid,$mid){
        $gItem=mGetGuestItem($gid);
        return mfa(mq("select * from data_player where openid='$gItem[openid]' and `mid`='$mid'"));
    }
    //修改参赛选手数据
    function mSetPlayerItem($id,$vars){
        mq("update data_player set ".mMakeSet($vars)." where id=".intval($id));
    }
    //获取省份选项
    function mGetProvinces(){
        return mq("select 省份 from data_city group by 省份 order by 省份");
    }
    //获取城市选项
    function mGetCityByProvince($province){
        return mq("select 城市 from data_city where 省份='$province' group by 城市 order by 城市");
    }
    //积分排行榜
    function mGetPointsTopList($mid,$count=10){
        return mq("select * from data_player where `mid`='$mid' order by 1*积分 desc limit 0,10");
    }
    //热门排行榜
    function mGetMatchTopList($mid,$count=9,$pageIndex=0){
        return mq("select * from data_player where `mid`='$mid' order by 1*票数 desc limit ".($pageIndex*$count).",$count");
    }
    //获取选手在比赛中第几名
    function mGetMatchPlayerIndex($mid,$pid){
        $player=mGetPlayerItem($pid);
        $row=mfa(mq("select count(*) from data_player where `mid`='$mid' and 1*票数>".intval($player[票数]).""));
        return $row[0]+1;
    }
    //评论排行榜 - 前 3 个是最热评论，其他是按时间越新越前
    function mGetMsgsTopList($mid){
        $sql="select a.内容,a.id,a.创建时间,a.票数,g.头像 from
                (
                    (select * from data_msgs where `mid`='$mid' order by 1*票数 desc limit 0,3)
                      UNION
                    (select * from data_msgs where `mid`='$mid' order by id desc limit 0,500)
                ) as a,data_guest as g where a.gid=g.id
                
                ";
        return mq($sql);
    }
    //插入评论记录
    function mAddMsgItem($gid,$mid,$pid,$content){
        mq("insert into data_msgs(gid,mid,pid,内容,创建时间) values ('$gid','$mid','$pid','$content','".now()."')");
        if($pid>0){ mq("update data_player set 评论数=1*评论数+1 where id=$pid"); }
    }
    //对评论点赞
    function mAddMsgVote($gid,$msgid){
        if(!mfa(mq("select * from data_vote_msg_log where gid='$gid' and msgid='$msgid'"))){
            mq("insert into data_vote_msg_log(gid,msgid,创建时间) values ('$gid','$msgid','".now()."')");
            mq("update data_msgs set 票数=1*票数+1 where id=$msgid");
            return true;
        }else{ return false; }
    }
    //对选手投票
    function mAddVote($gid,$pid,$mid){
        $item=mfa(mq("select count(*) from data_vote_log where gid='$gid' and `mid`='$mid' and 创建时间>='".date("Y-m-d")."'"));
        if($item[0]<3){
            mq("insert into data_vote_log(gid,pid,`mid`,创建时间) values ('$gid','$pid','$mid','".now()."')");
            mq("update data_player set 票数=1*票数+1 where id=$pid");
            if($your=mGetPlayerItemByGidMid($gid,$mid)){
                mAddPlayerPoint($your[id],2); //投票人得2积分
                if($your[邀请人]>0){ mAddPlayerPoint($your[邀请人],1); } //投票时我的邀请人得1积分
            }
            return true;
        }else{ return false; }
    }
    //获得我今天本次比赛的剩余票数
    function mGetMyRemainInMatch($mid){
        $you=mGetMyGuestItem();
        $item=mfa(mq("select count(*) from data_vote_log where gid='$you[id]' and `mid`='$mid' and 创建时间>='".date("Y-m-d")."'"));
        return (3-$item[0]);
    }
    //获得我今天本次比赛的剩余票数
    function mAddPlayerPoint($pid,$point=0){
        if($pid>0 && $point!=0){
            mq("update data_player set 积分=(1*积分)+(1*'".intval($point)."') where id=$pid");
        }
    }
    //获取我的比赛
    function mGetMyMatchList($guest){
        $sql="
                (select * from data_player as p,data_match as m where p.openid='$guest[openid]' and p.`mid`=m.id and m.状态='进行中')
                  UNION
                (select * from data_player as p,data_match as m where p.openid='$guest[openid]' and p.`mid`=m.id and m.状态='可报名')
                  UNION
                (select * from data_player as p,data_match as m where p.openid='$guest[openid]' and p.`mid`=m.id and m.状态='已结束')
                ";
        return mq($sql);
    }
    